package it.ipermemory;

import it.ipermemory.graphic.CustomPopup;
import it.ipermemory.logic.Thing;
import it.ipermemory.logic.Game;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Activity that represents the description of a Thing. You can see all your
 * data: images, text, audio, video etc..
 * 
 * @author Gianluca Carlesso (gianluca.carlesso@gmail.com)
 * @version 1.0
 */
public class CardActivity extends Activity {

	/**
	 * Thing to display
	 */
	private Thing thing;

	/**
	 * An object that initializes the game screen
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_card);

		// Data recovery Thing
		thing = Game.parserXML.getThingById(getIntent().getExtras().getInt(
				Game.GAME_THING_CARD));

		if (thing == null) {
			// If it is null I get a message error
			CustomPopup cp = new CustomPopup(CardActivity.this, getResources()
					.getString(R.string.error_media_load)) {

				@Override
				public void onClick(View v) {
					this.dismiss();
					finish();
				}
			};

			cp.getNoButton().setVisibility(View.GONE);
			cp.show();
			cp.setBackgroundDrawable(getResources().getDrawable(
					R.drawable.background));
		} else {
			// Set name of thing
			TextView name_thing = (TextView) findViewById(R.id.name_thing);
			name_thing.setText(thing.getName());

			// Set custom font
			Typeface typface = Typeface.createFromAsset(getAssets(),
					"fonts/wayoshi.ttf");

			name_thing.setTypeface(typface);

			TextView name_thing_title = (TextView) findViewById(R.id.name_thing_title);
			name_thing_title.setTypeface(typface);

			// Custom description
			typface = Typeface.createFromAsset(getAssets(), "fonts/dimbo.ttf");

			TextView description = (TextView) findViewById(R.id.description_thing);
			description.setText(thing.getDescription());
			description.setTypeface(typface);

			// Set image
			int resIDBackBox = getResources().getIdentifier(
					thing.getImage_full_screen(), "drawable", getPackageName());

			((ImageView) findViewById(R.id.image_card))
					.setImageBitmap(BitmapFactory.decodeResource(
							getResources(), resIDBackBox));
		}
	}

	/**
	 * Start the activity for audio playback
	 * 
	 * @param view
	 *            object on which the click was performed
	 */
	public void playSound(View view) {
		Intent intent = new Intent(this, SoundActivity.class);
		intent.putExtra(Game.GAME_THING_CARD, thing.getId());
		startActivity(intent);
	}

	/**
	 * Start the activity for video playback
	 * 
	 * @param view
	 *            object on which the click was performed
	 */
	public void playVideo(View view) {
		Intent intent = new Intent(this, VideoActivity.class);
		intent.putExtra(Game.GAME_THING_CARD, thing.getId());
		startActivity(intent);
	}
}
